package cn.iocoder.yudao.module.resource.service.messagecollection;

import cn.hutool.core.util.IdUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.resource.api.IMessageCollectionApi;
import cn.iocoder.yudao.module.resource.api.enums.EnumAipError;
import cn.iocoder.yudao.module.resource.api.enums.MessageSearchTypeEnum;
import cn.iocoder.yudao.module.resource.api.enums.MessageSourceTypeEnum;
import cn.iocoder.yudao.module.resource.api.model.messagecollection.MessageCollectionDTO;
import cn.iocoder.yudao.module.resource.api.model.messagecollection.MessageCollectionResDTO;
import cn.iocoder.yudao.module.resource.controller.admin.messagecollection.vo.MessageCollectionPageReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.messagecollection.vo.MessageCollectionSaveReqVO;
import cn.iocoder.yudao.module.resource.dal.dataobject.messagecollection.MessageCollectionDO;
import cn.iocoder.yudao.module.resource.dal.mysql.messagecollection.MessageCollectionMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 消息仓库 Service 实现类
 *
 * @author lizhongwen
 */
@Slf4j
@Service
@Validated
public class MessageCollectionServiceImpl implements MessageCollectionService {

    @Resource
    private MessageCollectionMapper messageCollectionMapper;
    @Resource
    private IMessageCollectionApi messageVectorApi;

    @Override
    public String createMessageCollection(MessageCollectionSaveReqVO createReqVO) {
        final String userText = createReqVO.getUserText();
        final String replyText = createReqVO.getReplyText();

        // 计算向量
        final Map<String, List<? extends Number>> vectorValues = messageVectorApi.getVectorValues(userText, replyText);

        // 插入
        final String messageId = IdUtil.objectId();
        messageVectorApi.insertMessage(null, MessageSourceTypeEnum.VECTOR, messageId,
                null, userText, replyText, vectorValues
        );

        // 返回
        return messageId;
    }

    @Override
    public void updateMessageCollection(MessageCollectionSaveReqVO updateReqVO) {
        // 校验存在
        validateMessageCollectionExists(updateReqVO.getMessageId());

        final String userText = updateReqVO.getUserText();
        final String replyText = updateReqVO.getReplyText();

        // 计算向量
        final Map<String, List<? extends Number>> vectorValues = messageVectorApi.getVectorValues(userText, replyText);

        // 更新
        messageVectorApi.updateMessage(
                null, MessageSourceTypeEnum.VECTOR, updateReqVO.getMessageId(),
                null, userText, replyText, vectorValues
        );
    }

    @Override
    public void deleteMessageCollection(String id) {
        // 校验存在
        validateMessageCollectionExists(id);

        // 删除
        messageVectorApi.deleteMessage(id);
    }

    private MessageCollectionDO validateMessageCollectionExists(String id) {
        MessageCollectionDO messageCollectionDO = messageCollectionMapper.selectById(id);
        if (messageCollectionDO == null) {
            throw exception(EnumAipError.MESSAGE_COLLECTION_NOT_EXISTS);
        }
        return messageCollectionDO;
    }

    @Override
    public MessageCollectionDO getMessageCollection(String id) {
        return messageCollectionMapper.selectById(id);
    }

    @Override
    public PageResult<MessageCollectionDO> getMessageCollectionPage(MessageCollectionPageReqVO pageReqVO) {
        final MessageSearchTypeEnum searchType = MessageSearchTypeEnum.valueOf(pageReqVO.getSearchType());
        if (searchType == MessageSearchTypeEnum.VECTOR) {
            // 向量搜索
            final Integer similarity = pageReqVO.getSimilarity();
            if (similarity == null) {
                throw exception(EnumAipError.MESSAGE_COLLECTION_DEFAULT_ERROR, "similarity不能为空");
            }

            final String userText = pageReqVO.getUserText();
            final String replyText = pageReqVO.getReplyText();

            final MessageCollectionResDTO messageVectorRes = messageVectorApi.queryMessage(similarity / 100f, 100, userText, replyText);
            final List<MessageCollectionDTO> messages = messageVectorRes.getMessages();
            return new PageResult<>(BeanUtils.toBean(messages, MessageCollectionDO.class), Long.valueOf(messages.size()));
        } else {
            // 数据库搜索
            return messageCollectionMapper.selectPage(pageReqVO);
        }
    }

}